"""
管理视图层
"""
from interface import admin_interface
from lib import common
from interface import common_interface
#登陆状态
admin_dic = {
    "user":None
}

#注册功能
def regiter():
    while True:
        user_name = input("请输入用户名:")
        password = input("请输入密码:")
        repassword = input("再输入一次密码:")
        if password != repassword:
            print("俩次密码不同，请在此输入！")
            continue 
        flag,msg = admin_interface.register(user_name,password)
        if flag:
            print(msg)
            break
        else:
            print(msg)

#登陆功能
def login():
    while True:
        name = input("请输入用户账号:")
        password = input("请输入用户密码:")
        flag,msg = common_interface.login(name,password,type ="admin")
        if flag:
            admin_dic["user"] = name
            print(msg)
            break
        else:
            print(msg)

#创建学校
@common.login("admin")
def create_school():
    while True:
        name = input("请输入学校名称:")
        addr = input("请输入学校地址:")
        flag,msg = admin_interface.create_school(name,addr,admin_dic["user"])
        if flag:
            print(msg)
            break
        else:
            print(msg)
            
#创建课程
@common.login("admin")
def create_crouse():
    while True:
        crouse_name = input("请输入课程名:")
        crouse_price = input("请输入课程价格:")
        crouse_week = input("请输入课程周期:")
        flag,msg = common_interface.select_school()
        if not flag:
            print(msg)
            break
        else:
            school_list = msg
        for index,school_name in enumerate(school_list):
            print(f"编号{index}---学校名称{school_name}")  
        choice = input("请选择编号对应的学校:")
        if not choice.isdigit():
            print("请输入正确的编号")
            continue 
        choice = int(choice)
        if choice not in range(len(school_list)):
            print("请输入正确的编号")
            continue 
        school_choice = school_list[choice]
        flag,msg = admin_interface.create_crouse(crouse_name,crouse_price,crouse_week,admin_dic["user"],school_choice)
        if flag:
            print(msg)
            break
        else:
            print(msg)

#创建教师
@common.login("admin")
def create_teather():
    while True:
        teather_name = input("请输入老师的名字：")
        flag,msg = admin_interface.create_teather(teather_name,admin_dic["user"])
        if flag:
            print(msg)
            break
        else:
            print(msg)
func_dic={
    "1":regiter,
    "2":login,
    "3":create_school,
    "4":create_crouse,
    "5":create_teather,
}

def admin_view():
    while True:
        print("""
        =================
            1.注册
            2.登录
            3.创建学校
            4.创建课程
            5.创建讲师
        ==================
        """)
        choice = input("请按编号选择一个功能(输入q退出此功能):")
        if choice == "q":
            break
        if choice  not in func_dic:
            print("请输入正确得编号")
            continue
        func_dic.get(choice)()